﻿using System;
using System.Data;
using msBsc.AppLogic.DataAlc;
using System.Text;

namespace msBsc.WebUi
{
    public partial class wfAddRecommendation : msBsc.Cls.BPageHt
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }

        private void BindControls()
        {
            if (!IsPostBack)
            {
            }
        }
        #region 地区信息
        private void showPlace1()
        {
            string parentsId = this.GetParamHtValue("parentsId");

            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                int listNum = ds.Tables[0].Rows.Count;
                StringBuilder[] cityArray = packageCityArray(ds);
                StringBuilder listValue = new StringBuilder();
                StringBuilder listOption = new StringBuilder();
                listValue = cityArray[0];
                listOption = cityArray[1];
                this.ExecJScript(@"setArea('outerInner',200,'1'," + listNum.ToString() + "," + listValue.ToString() + "," + listOption.ToString() + ");");
                showPlace2(ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString());
            }

        }

        private StringBuilder[] packageCityArray(DataSet ds)
        {
            StringBuilder[] cityArray = new StringBuilder[2];
            StringBuilder listValue = new StringBuilder();
            StringBuilder listOption = new StringBuilder();
            listValue.Append(@"[");
            listOption.Append(@"[");
            bool isFirst = true;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (isFirst)
                {
                    isFirst = false;
                }
                else
                {
                    listValue.Append(",");
                    listOption.Append(",");
                }
                listValue.Append(@"""" + ds.Tables[0].Rows[i]["COUNTRY_CITY_ID"] + @"""");
                listOption.Append(@"""" + ds.Tables[0].Rows[i]["COUNTRY_CITY_NME"] + @"""");
            }
            listValue.Append(@"]");
            listOption.Append(@"]");
            cityArray[0] = listValue;
            cityArray[1] = listOption;
            return cityArray;
        }



        private void showPlace2(string strPlace1Id)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(strPlace1Id);
                int listNum = ds.Tables[0].Rows.Count;
                StringBuilder[] cityArray = packageCityArray(ds);
                StringBuilder listValue = new StringBuilder();
                StringBuilder listOption = new StringBuilder();
                listValue = cityArray[0];
                listOption = cityArray[1];
                this.ExecJScript(@"setArea('province',200,'2'," + listNum.ToString() + "," + listValue.ToString() + "," + listOption.ToString() + ");");
                showPlace3(ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString());
            }
        }
        private void showPlace3(string strPlace2Id)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(strPlace2Id);
                string isAll = this.GetParamHtValue("isAll");
                if (isAll.ToLower() == "true")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["COUNTRY_CITY_ID"] = "";
                    dr["COUNTRY_CITY_NME"] = "全部";
                    ds.Tables[0].Rows.InsertAt(dr, 0);
                }
                int listNum = ds.Tables[0].Rows.Count;
                StringBuilder[] cityArray = packageCityArray(ds);
                StringBuilder listValue = new StringBuilder();
                StringBuilder listOption = new StringBuilder();
                listValue = cityArray[0];
                listOption = cityArray[1];
                this.ExecJScript(@"setArea('city',200,'3'," + listNum.ToString() + "," + listValue.ToString() + "," + listOption.ToString() + ");");
            }
        }
        private void initplace()
        {
            showPlace1();
        }
        private void getArea1(string parentsId)
        {

            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                StringBuilder selectData = new StringBuilder();
                selectData.Append(@"[");
                bool isFirst = true;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        selectData.Append(",");
                    }
                    string tempData = @"{""ID"":""$ID$"",""Name"":""$Name$""}";
                    tempData = tempData.Replace("$ID$", ds.Tables[0].Rows[i]["COUNTRY_CITY_ID"].ToString());
                    tempData = tempData.Replace("$Name$", ds.Tables[0].Rows[i]["COUNTRY_CITY_NME"].ToString());
                    selectData.Append(tempData);
                }
                selectData.Append(@"]");
                this.ExecJScript(@"updateSelet('outerInner'," + selectData.ToString() + ");");
                getArea2(ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString());
            }
        }

        private void getArea2(string parentsId)
        {

            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                StringBuilder selectData = new StringBuilder();
                selectData.Append(@"[");
                bool isFirst = true;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        selectData.Append(",");
                    }
                    string tempData = @"{""ID"":""$ID$"",""Name"":""$Name$""}";
                    tempData = tempData.Replace("$ID$", ds.Tables[0].Rows[i]["COUNTRY_CITY_ID"].ToString());
                    tempData = tempData.Replace("$Name$", ds.Tables[0].Rows[i]["COUNTRY_CITY_NME"].ToString());
                    selectData.Append(tempData);
                }
                selectData.Append(@"]");
                this.ExecJScript(@"updateSelet('province'," + selectData.ToString() + ");");
                getArea3(ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString());
            }
        }
        private void getArea3(string getArea3Id)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                string isAll = this.GetParamHtValue("isAll");
                DataSet ds = cbcc.SelectCountryByParentId(getArea3Id);
                StringBuilder selectData = new StringBuilder();
                selectData.Append(@"[");
                bool isFirst = true;
                if (isAll.ToLower() == "true")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["COUNTRY_CITY_ID"] = "";
                    dr["COUNTRY_CITY_NME"] = "全部";
                    ds.Tables[0].Rows.InsertAt(dr, 0);
                }
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        selectData.Append(",");
                    }
                    string tempData = @"{""ID"":""$ID$"",""Name"":""$Name$""}";
                    tempData = tempData.Replace("$ID$", ds.Tables[0].Rows[i]["COUNTRY_CITY_ID"].ToString());
                    tempData = tempData.Replace("$Name$", ds.Tables[0].Rows[i]["COUNTRY_CITY_NME"].ToString());
                    selectData.Append(tempData);
                }
                selectData.Append(@"]");
                this.ExecJScript(@"updateSelet('city'," + selectData.ToString() + ");");
            }
        }
        #endregion
        public override void DoAction()
        {
            if (this.strAction == "getArea2")
            {
                string parentsId = this.GetParamHtValue("parentsId");
                getArea2(parentsId);
            }
            else if (this.strAction == "getArea3")
            {
                string parentsId = this.GetParamHtValue("parentsId");
                getArea3(parentsId);
            }
            else if (this.strAction == "initplace")
            {
                string parentsId = this.GetParamHtValue("parentsId");
                getArea1(parentsId);
            }
            else if (this.strAction == "AddRecomm")
            {
                AddRecommendation();
            }
        }

        /// <summary>
        /// 添加资讯
        /// </summary>
        private void AddRecommendation()
        {
            int recommType = string.IsNullOrEmpty(this.GetParamHtValue("selectType")) ? 0 : int.Parse(this.GetParamHtValue("selectType"));
            string desId = this.GetParamHtValue("destinationId");
            string cityId = this.GetParamHtValue("cityId");
            string countryId = this.GetParamHtValue("countryId");
            string recommTitle = this.GetParamHtValue("recommTitle");
            string recommAddr = this.GetParamHtValue("recommAddr");
            string recommPrice = this.GetParamHtValue("recommPrice");
            string recommPic = this.GetParamHtValue("recommPic");
            bool recommIsPush = this.GetParamHtValue("recommIsPush") == "1" ? true : false;
            string recommContent = this.GetParamHtValue("recommContent");

            recommPic = recommPic.Split('/')[recommPic.Split('/').Length - 1];

            if (recommType == 0)
            {
                ExecJsToShowMsg("true", "资讯类型有误，请确认！");
                return;
            }



            string[] result = new string[2];
            switch (recommType)
            {
                case 1:
                    using (msBsc.AppLogic.DataAlc.CBizPushScenic objCBizPushScenic = new CBizPushScenic())
                    {
                        result = objCBizPushScenic.AddScenicInfo(recommTitle, recommAddr, recommPrice, recommIsPush, recommPic, desId, cityId, countryId, recommContent);
                    }
                    break;
                case 2:
                    using (msBsc.AppLogic.DataAlc.CBizPushNiceFood objCBizPushNiceFood = new CBizPushNiceFood())
                    {
                        result = objCBizPushNiceFood.AddNiceFoodInfo(recommTitle, recommAddr, recommPrice, recommIsPush, recommPic, desId, cityId, countryId, recommContent);
                    }
                    break;
                case 3:
                    using (msBsc.AppLogic.DataAlc.CBizPushSpecialty objCBizCBizPushSpecialty = new CBizPushSpecialty())
                    {
                        result = objCBizCBizPushSpecialty.AddSpecialtyInfo(recommTitle, recommAddr, recommPrice, recommIsPush, recommPic, desId, cityId, countryId, recommContent);
                    }
                    break;
            }

            if (result[0] == "true")
            {
                ExecJsToShowMsg("false", result[1]);
            }
            else
            {
                ExecJsToShowMsg("true", result[1]);
            }
        }

        private void ExecJsToShowMsg(string IsErr, string Msg)
        {
            this.ExecJScript("ShowMessage('" + IsErr + "','" + Msg + "')");
            this.ExecJScript("PageReset()");
        }
    }
}